package com.pcafs.manage.controller;

import com.pcafs.common.annotation.Log;
import com.pcafs.common.core.controller.BaseController;
import com.pcafs.common.core.domain.AjaxResult;
import com.pcafs.common.core.page.TableDataInfo;
import com.pcafs.common.enums.BusinessType;
import com.pcafs.common.utils.poi.ExcelUtil;
import com.pcafs.manage.bo.ActivityAddBo;
import com.pcafs.manage.bo.ActivityEditBo;
import com.pcafs.manage.bo.ActivityQueryBo;
import com.pcafs.manage.domain.ActUser;
import com.pcafs.manage.domain.Activity;
import com.pcafs.manage.service.IActivityService;
import com.pcafs.manage.vo.ActivityVo;
import com.pcafs.manage.vo.CollegeScore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 活动Controller
 * 
 * @author pcafs
 * @date 2021-05-03
 */
@Api(value = "活动控制器", tags = {"活动管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/manage/activity")
public class ActivityController extends BaseController {

    private final IActivityService iActivityService;

    /**
     * 查询活动列表
     */
    @ApiOperation("查询活动列表")
    @PreAuthorize("@ss.hasPermi('manage:activity:list')")
    @GetMapping("/list")
    public TableDataInfo<ActivityVo> list(ActivityQueryBo bo) {
        startPage();
        List<ActivityVo> list = iActivityService.queryList(bo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('manage:activity:list')")
    @GetMapping("/listVo")
    public TableDataInfo<Activity> listVo(Activity activity) {
        startPage();
        List<Activity> activities = iActivityService.queryListVo(activity);
        return getDataTable(activities);
    }

    /**
     * 导出活动列表
     */
    @ApiOperation("导出活动列表")
    @PreAuthorize("@ss.hasPermi('manage:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ActivityVo> export(ActivityQueryBo bo) {
        List<ActivityVo> list = iActivityService.queryList(bo);
        ExcelUtil<ActivityVo> util = new ExcelUtil<ActivityVo>(ActivityVo.class);
        return util.exportExcel(list, "活动");
    }

    /**
     * 获取活动参加者
     * @param id 活动ID
     * @return
     */
    @ApiOperation("获取活动参加者")
    @GetMapping("/getActUsers/{id}")
    public AjaxResult<List<ActUser>> getActUsers(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iActivityService.getActUsers(id));
    }


    @ApiOperation("获取活动详细信息")
    @PreAuthorize("@ss.hasPermi('manage:activity:query')")
    @GetMapping("/{id}")
    public AjaxResult<ActivityVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iActivityService.queryById(id));
    }

    /**
     *  获取活动结果
     * @param deptId 学院 ID
     * @return
     */
    @GetMapping("/getCollegeActResult")
    public TableDataInfo<CollegeScore> getCollegeActResult(Long deptId) {
        startPage();
        List<CollegeScore> collegeActResult = iActivityService.getCollegeActResult(deptId);
        return getDataTable(collegeActResult);
    }

    @GetMapping("/getAllActResult")
    public TableDataInfo<CollegeScore> getAllActResult() {
        startPage();
        List<CollegeScore> allActResult = iActivityService.getAllActResult();
        return getDataTable(allActResult);
    }

    /**
     * 新增活动
     */
    @ApiOperation("新增活动")
    @PreAuthorize("@ss.hasPermi('manage:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody ActivityAddBo bo) {
        return toAjax(iActivityService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 活动打分
     * @param list
     * @return
     */
    @PostMapping("/scoring")
    public AjaxResult<Void> scoring(@RequestBody List<ActUser> list) {
        return toAjax(iActivityService.scoring(list));
    }

    @ApiOperation("报名参加活动")
    @PostMapping("/joinAct")
    public AjaxResult<Integer> joinAct(@RequestBody ActUser actUser) {
        return AjaxResult.success(iActivityService.joinAct(actUser));
    }

    /**
     * 修改活动
     */
    @ApiOperation("修改活动")
    @PreAuthorize("@ss.hasPermi('manage:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody ActivityEditBo bo) {
        return toAjax(iActivityService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 修改活动状态
     * @param actId
     * @return
     */
    @PutMapping("/updateActState")
    public AjaxResult<Void> updateActState(@RequestBody Long actId) {
        return toAjax(iActivityService.updateActState(actId));
    }

    /**
     * 删除活动
     */
    @ApiOperation("删除活动")
    @PreAuthorize("@ss.hasPermi('manage:activity:remove')")
    @Log(title = "活动" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iActivityService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
